<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-popconfirm
          title="是否确认失效？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleInvalid()"
        >
          <a-button type="default" v-auth="'shop:gift:shopGiftData:dis'">
            <Icon icon="ant-design:close-circle-outlined" /> {{ t('失效') }}
          </a-button>
        </a-popconfirm>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'shop:gift:shopGiftData:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.name }}
        </a>
      </template>
      <template #picUrl="{ record }">
        <img :src="record.picUrl" width="50" height="50" />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopGiftShopGiftDataList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { shopGiftDataDelete, shopGiftDataListData, shopGiftDataDisStatus } from '/@/api/shop/gift/shopGiftData';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('shop.gift.shopGiftData');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('即买即赠流水管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('政策名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('日期'),
        field: 'ddate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
          showTime: false,
          // 只显示日期
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('经销商'),
        field: 'basCus.name',
        component: 'Input',
      },
      {
        label: t('物料编码'),
        field: 'invCode',
        component: 'Input',
      },

      {
        label: t('生效日期'),
        field: 'effDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          // showTime: { format: 'HH:mm' },
          showTime: false,
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('失效日期'),
        field: 'expDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          // showTime: { format: 'HH:mm' },
          showTime: false,
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'btdm_gift_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('政策名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 230,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('经销商'),
      dataIndex: 'basCus.name',
      // key: 'a.cus_code',
      // sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('简图'),
      dataIndex: 'basInv.picUrl',
      key: 'a.pic_url',
      // sorter: true,
      width: 130,
      align: 'left',
      slot: 'picUrl',
    },
    {
      title: t('物料编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      // sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('物料名称'),
      dataIndex: 'basInv.cinvname',
      // key: 'a.inv_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('已带走数量'),
      dataIndex: 'useQty',
      key: 'a.use_qty',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('单位'),
      dataIndex: 'basInv.csacomunitname',
      // key: 'a.inv_code',
      sorter: true,
      width: 60,
      align: 'left',
    },
    {
      title: t('单价'),
      dataIndex: 'price',
      key: 'a.price',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('价值'),
      dataIndex: 'amount',
      key: 'a.amount',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('生效日期'),
      dataIndex: 'effDate',
      key: 'a.eff_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('失效日期'),
      dataIndex: 'expDate',
      key: 'a.exp_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'btdm_gift_status',
    },
    {
      title: t('修改日期'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: shopGiftDataListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/shop/gift/shopGiftData/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await shopGiftDataDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  async function handleInvalid() {
    if (!getSelectRowKeys().length) {
      showMessage(t('请选择要失效的数据'));
      return;
    }
    const res = await shopGiftDataDisStatus({
      selIds: getSelectRowKeys().join(','),
    });
    showMessage(res.message);
    handleSuccess();
  }
</script>
